package com.leyou.item.service.impl;

import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.mapper.SpecificationMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private SpecificationMapper specificationMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    /**
     * 根据分类查询查询规格参数组(规格参数组和分类是关联的)
     *
     * @param cid
     * @return
     */
    @Override
    public List<SpecGroup> querySpecGroups(Long cid) {
        SpecGroup record = new SpecGroup();
        record.setCid(cid);
        List<SpecGroup> specGroups = specificationMapper.select(record);
        //查询规格组的同时,将规格参数组的组内参数查询出来
        specGroups.forEach(specGroup -> {
            SpecParam specParam = new SpecParam();
            specParam.setGroupId(specGroup.getId());
            specParam.setCid(cid);
            List<SpecParam> SpecParam = specParamMapper.select(specParam);
            specGroup.setSpecParams(SpecParam);
        });
        return specGroups;
    }

    /**
     * 规格组的修改
     *
     * @param specGroup
     */
    @Override
    public void saveSpecGroups(SpecGroup specGroup) {
        this.specificationMapper.updateByPrimaryKey(specGroup);
    }

    /**
     * 规格的删除
     *
     * @param id
     */
    @Override
    public void deleteSpecGroups(Long id) {
        this.specificationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 规格组的删除
     *
     * @param specGroup
     */
    @Override
    public void addSpecGroups(SpecGroup specGroup) {
        this.specificationMapper.insert(specGroup);
    }

    /**
     * 从规格参数组到规格参数
     *
     * @param gid
     * @return
     */
    @Override
    public List<SpecParam> querySpecParams(Long gid, Long cid, Boolean searching,Boolean generic) {
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        specParam.setGeneric(generic);
        List<SpecParam> list = this.specParamMapper.select(specParam);
        return list;
    }
}
